<?php

namespace App\Dao;


use App\Controller\AbstractController;
use App\Model\AnchorEvaluateData;
use App\Utils\LoggerUtil;
use App\Utils\MemcacheClient;
use function Hyperf\Translation\trans;

class AnchorEvaluateDataDao extends AbstractController
{
    public function getEvaluateByAnchor($anchor_id, $language = 'en')
    {
        $result = [];
        $data = $this->getAllEvaluateByUser($anchor_id);
        // LoggerUtil::write('an',$data);
        $anchorEvaluateDao = new AnchorEvaluateDao();
        $evaluate = $anchorEvaluateDao->getData(-1, 1);
        // LoggerUtil::write('an',$evaluate);
        foreach ($data as $k => $v) {
            if ($language == 'en') {
                if (isset($evaluate[$v['eid']]['en_content']) && !empty($evaluate[$v['eid']]['en_content'])) {
                    $result[] = array_merge($v, ['content' => $evaluate[$v['eid']]['en_content'], 'color' => $evaluate[$v['eid']]['color'], 'bg_color' => $evaluate[$v['eid']]['bg_color']]);
                }
            } elseif ($language == 'zh-cn') {
                if (isset($evaluate[$v['eid']]['content']) && !empty($evaluate[$v['eid']]['content'])) {
                    $result[] = array_merge($v, ['content' => $evaluate[$v['eid']]['content'], 'color' => $evaluate[$v['eid']]['color'], 'bg_color' => $evaluate[$v['eid']]['bg_color']]);
                }
            } elseif ($language == 'zh-hk') {
                if (isset($evaluate[$v['eid']]['content']) && !empty($evaluate[$v['eid']]['content'])) {
                    $result[] = array_merge($v, ['content' => trans('evaluate.' . $evaluate[$v['eid']]['content'], [], 'zh-hk'), 'color' => $evaluate[$v['eid']]['color'], 'bg_color' => $evaluate[$v['eid']]['bg_color']]);
                }
            }
        }
        // LoggerUtil::write('an',$result);
        // 填充数组长度到3
        if (count($result) < 3) {
            $evaluate_ids = array_diff(array_keys($evaluate), array_column($result, 'eid'));
            // LoggerUtil::write('an',$evaluate_ids);
            $round_ids = array_rand($evaluate_ids, 3 - count($result));
            // LoggerUtil::write('an',$round_ids);
            if (!is_array($round_ids) || count($round_ids) == 1) {
                $round_ids = [$round_ids];
            }
            foreach ($round_ids as $value) {
                $round_id = $evaluate_ids[$value];
                if ($language == 'en') {
                    if (isset($evaluate[$round_id]['en_content']) && !empty($evaluate[$round_id]['en_content'])) {
                        $result[] = ['eid' => (string)$round_id, 'total' => '1', 'content' => $evaluate[$round_id]['en_content'], 'color' => $evaluate[$round_id]['color'], 'bg_color' => $evaluate[$round_id]['bg_color']];
                    }
                } elseif ($language == 'zh-cn') {
                    if (isset($evaluate[$round_id]['content']) && !empty($evaluate[$round_id]['content'])) {
                        $result[] = ['eid' => (string)$round_id, 'total' => '1', 'content' => $evaluate[$round_id]['content'], 'color' => $evaluate[$round_id]['color'], 'bg_color' => $evaluate[$round_id]['bg_color']];
                    }
                } elseif ($language == 'zh-hk') {
                    if (isset($evaluate[$round_id]['content']) && !empty($evaluate[$round_id]['content'])) {
                        $result[] = ['eid' => (string)$round_id, 'total' => '1', 'content' => trans('evaluate.' . $evaluate[$round_id]['content'], [], 'zh-hk'), 'color' => $evaluate[$round_id]['color'], 'bg_color' => $evaluate[$round_id]['bg_color']];
                    }
                }
            }
        }
        if (count($result) < 3) {
            LoggerUtil::write('an', $evaluate);
        }
        return $result;
    }

    // 获取该用户收到的各个评价的数量
    public function getAllEvaluateByUser($uid)
    {
        // $cache = MemcacheClient::getInstance()->get('anchor_evaluation_all_'.$uid);
        if (empty($cache)) {
            $cache = AnchorEvaluateData::query()->selectRaw('eId eid,sum(num) as total')->where('anchor_id', $uid)
                ->groupBy('eid')->orderBy('total', 'desc')
                ->get();
            if ($cache) {
                $cache = $cache->toArray();
            }
            // MemcacheClient::getInstance()->set('anchor_evaluation_all_'.$uid, $cache, 600);
        }
        return $cache;
    }

}